<?php
/**
 * Created by PhpStorm.
 * User: zhangrongxiang
 * Date: 2018/2/10
 * Time: 下午9:40
 */

$arr = [
	[
		"id"   => 1,
		"name" => "zing",
		"age"  => 23
	],
	[
		"id"   => 2,
		"name" => "zhangrxiang",
		"age"  => 24
	],
	[
		"id"   => 3,
		"name" => "zrx",
		"age"  => 24
	]
];
/**
 * Array(
 * [0] => zing
 * [1] => zhangrxiang
 * [2] => zrx
 * )
 */
$column = array_column( $arr, 'name' );
print_r( $column );

/**
 * Array(
 * [1] => zing
 * [2] => zhangrxiang
 * [3] => zrx
 * )
 */
$column = array_column( $arr, 'name', 'id' );
print_r( $column );
echo $column[1] . PHP_EOL;

//////////////////////////////////////////////////////////////
class User {
	public $username;
	
	public function __construct( string $username ) {
		$this->username = $username;
	}
}

$users = [
	new User( 'user 1' ),
	new User( 'user 2' ),
	new User( 'user 3' ),
];
print_r( array_column( $users, 'username' ) );


function array_column2( $array, $column_name ) {
	return array_map( function ( $element ) use ( $column_name ) {
		return $element[ $column_name ];
	}, $array );
	
}

//Array
//(
//	[0] => zing
//	[1] => zhangrxiang
//[2] => zrx
//)
print_r( array_column2( $arr, 'name' ) );